package com.azunitech.jms.ejb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

import com.azunitech.jms.common.IPartRepository;
import com.azunitech.jms.common.domains.PartsEntity;
import com.azunitech.jms.repos.PartsRepository;

@MessageDriven(mappedName = "Topic-1", activationConfig = {
		@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
		@ActivationConfigProperty(propertyName = "clientId", propertyValue = "TopicEJB"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/Topic-1"),
		@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "TopicEJB") })
public class TopicEJB implements MessageListener {
	private Logger s_Logger = Logger.getLogger(TopicEJB.class);	
	 public void onMessage(Message msg) {
		 try {
			 s_Logger.info(msg.getJMSDestination());
			
			 if (msg instanceof TextMessage) { 
				 TextMessage message = (TextMessage) msg;
				 s_Logger.info(message.getText());
			 } 
			 else if (msg instanceof MapMessage) {
				 MapMessage mapMessage = (MapMessage) msg;
				 s_Logger.info(mapMessage.getString("hello" ));
			 } 
			 else if (msg instanceof ObjectMessage) {
				 ObjectMessage objectMessage = (ObjectMessage) msg;
				 s_Logger.info(objectMessage.getObject());
			 } 
			 else if (msg instanceof BytesMessage) {
				 BytesMessage bytesMessage = (BytesMessage) msg;
				 s_Logger.info(bytesMessage.getStringProperty("byte"));
			 } 
			 else if (msg instanceof StreamMessage) {
				 StreamMessage streamMessage = (StreamMessage) msg;
				 s_Logger.info(streamMessage.getStringProperty("stream"));
			 }
			 
			 IPartRepository repo = new PartsRepository();
			 repo.init();
			 PartsEntity entity = repo.getParts("12345");
			 s_Logger.info(entity);
			 repo.close();
		 } 
		 catch (JMSException e) {
			 e.printStackTrace();
		 } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}
